namespace cnVery.Cms.Ctrl
{
    using cnVery.Data;
    using System;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class Categories : UserControl
    {
        protected int _cid;
        protected string _link;
        protected DataList cat1;

        private void BindCategories()
        {
            Connection connection = new Connection();
            this.cat1.DataSource = connection.GetDataTable("select * from categories where parentid = " + this.CategoryId + " order by sortid");
            this.cat1.DataBind();
            connection.Close();
        }

        private void cat1Roll(object sender, DataListItemEventArgs e)
        {
            DataRowView dataItem = (DataRowView) e.Item.DataItem;
            if (dataItem != null)
            {
                int num = (int) dataItem["id"];
                Repeater repeater = e.Item.FindControl("cat2") as Repeater;
                Connection connection = new Connection();
                repeater.DataSource = connection.GetDataTable("select * from categories where parentid = " + num.ToString() + " order by sortid");
                repeater.DataBind();
                connection.Close();
            }
        }

        private void InitializeComponent()
        {
            base.Load += new EventHandler(this.Page_Load);
            this.cat1.ItemCreated += new DataListItemEventHandler(this.cat1Roll);
        }

        protected override void OnInit(EventArgs e)
        {
            this.InitializeComponent();
            base.OnInit(e);
        }

        private void Page_Load(object sender, EventArgs e)
        {
            this.BindCategories();
        }

        public int CategoryId
        {
            get
            {
                return this._cid;
            }
            set
            {
                this._cid = value;
            }
        }

        public string Link
        {
            get
            {
                return this._link;
            }
            set
            {
                this._link = value;
            }
        }
    }
}

